import wsgiref.handlers
import os
import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
#import google.appengine.ext.webapp as webapp
import logging
import gdata.service
import GoogleService
import gdata.urlfetch
import CalendarPage
import sys
import gdata.docs.service

gdata.service.http_request_handler = gdata.urlfetch


class MainPage(webapp.RequestHandler):
    def get(self):
        logging.debug(sys.path)
        client = gdata.docs.service.DocsService()
        cal_auth = GoogleService.GoolgleServiceAuthenticator().GetAuthSubUrl(client)
        
        values = {}
        values['cal_url'] = cal_auth
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
            
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'
            calendar_feed = []
            
        values['url'] =url
        values['url_linktext'] = url_linktext
        
        path = os.path.join(os.path.dirname(__file__), "index.html")
        self.response.out.write(template.render(path, values, True))
        
        
def main():
  application = webapp.WSGIApplication([('/', MainPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
